# Change this file to be a wrapper around your daemon code.

# Do your post daemonization configuration here
# At minimum you need just the first line (without the block), or a lot
# of strange things might start happening...
DaemonKit::Application.running! do |config|
  # Trap signals with blocks or procs
  # config.trap( 'INT' ) do
  #   # do something clever
  # end
  # config.trap( 'TERM', Proc.new { puts 'Going down' } )
end
$config = DaemonKit::Config.hash('marcobot')
$network = 'network' #TODO: fix this
$ircbot = IRCBot.new($config,$network)
begin
  $ircbot.connect()
rescue Exception => e
  DaemonKit.logger.exception(e)
end
begin
  $ircbot.main_loop()
rescue Interrupt
  $ircbot.send("QUIT")
rescue SignalException
  $ircbot.send("QUIT")
rescue Exception => e
  DaemonKit.logger.exception(e)
  $ircbot.privmsg("#{$config['network']['debug_channel']},#{$config['network']['master_user']}","Got an exception: #{( e.message ? e.message : "Unknown")}")
  $ircbot.privmsg($config['network']['master_user'],"Backtrace: #{e.backtrace.join(", ")}")
  retry
end
